/*
 * Copyright 2010 Chad Retz
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.jdocng.util;

import java.text.BreakIterator;
import java.util.Locale;

/**
 * Utilities for handling javadoc things
 *
 * @author Chad Retz
 */
public class JavadocUtils {

    /**
     * Get a modifier specifier integer from the given modifier names
     * 
     * @param modifiers
     * @return
     */
    public static int getModifierFromNames(String... modifiers) {
        //TODO
        throw new UnsupportedOperationException();
    }
    
    /**
     * Determine whether the locale given (or default if
     * null) is an english locale. This is needed by
     * {@link #getFirstSentence(String, String)}.
     * 
     * @param locale If null, the default locale is used
     * @return
     */
    public static boolean isEnglishLocale(Locale locale) {
        return "English".equals(locale.getDisplayLanguage(
                Locale.US));
    }

    /**
     * Get the first sentence of a comment per
     * {@link com.sun.javadoc.Doc#firstSentenceTags()}
     * 
     * @param comment
     * @param locale The locale or null to use the default
     * @return
     */
    public static String getFirstSentence(String comment,
            String locale) {
        if (comment == null) {
            return "";
        }
        //TODO too heavy to be done here? should be cached somewhere...
        Locale lcl = locale == null ? Locale.getDefault() :
            new Locale(locale);
        if (!isEnglishLocale(lcl)) {
            BreakIterator iter = BreakIterator.getSentenceInstance(lcl);
            iter.setText(comment);
            return comment.substring(0, iter.first() + 1);
        }
        int endingIndex = -1;
        for (int i = 0; i < comment.length(); i++) {
            char chr = comment.charAt(i);
            //check period and followed by space, tab, or line end
            if (chr == '.' && comment.length() >= i) {
                char nextChar = comment.charAt(i + 1);
                if (nextChar == ' ' || nextChar == '\t' ||
                        nextChar == '\n' || nextChar == '\r') {
                    endingIndex = i;
                    break;
                }
            }
            //check html tag
            if (chr == '<') {
                int tagEndIndex = comment.indexOf('>', i);
                if (tagEndIndex != -1) {
                    String piece = comment.substring(i, tagEndIndex + 1);
                    if ("<p>".equalsIgnoreCase(piece) ||
                            "</p>".equalsIgnoreCase(piece) ||
                            "<h1>".equalsIgnoreCase(piece) ||
                            "<h2>".equalsIgnoreCase(piece) ||
                            "<h3>".equalsIgnoreCase(piece) ||
                            "<h4>".equalsIgnoreCase(piece) ||
                            "<h5>".equalsIgnoreCase(piece) ||
                            "<h6>".equalsIgnoreCase(piece) ||
                            "<hr>".equalsIgnoreCase(piece) ||
                            "<pre>".equalsIgnoreCase(piece) ||
                            "</pre>".equalsIgnoreCase(piece)) {
                        endingIndex = i - 1;
                        break;
                    }
                }
            }
        }
        if (endingIndex == -1) {
            return comment;
        } else {
            return comment.substring(0, endingIndex + 1);
        }
    }
    
    public static boolean isValidJavaName(String name, boolean allowDots) {
        for (int i = 0; i < name.length(); i++) {
            char chr = name.charAt(i);
            if (chr == '.' && !allowDots) {
                return false;
            } else if (chr != '.' &&
                    (i == 0 && !Character.isJavaIdentifierStart(chr)) ||
                    (i > 0 && !Character.isJavaIdentifierPart(chr))) {
                return false;
            }
        }
        return true;
    }
    
    private JavadocUtils() {
    }
}
